package 第三方工具类.hutool.加解密;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.jwt.signers.AlgorithmUtil;
import org.junit.Test;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.security.AlgorithmConstraints;

/**
 * Description
 *
 * @Author: SunAo
 * @Date: 2022/2/18 11:29
 */
public class AES使用 {


    @Test
    public void 加解密过程(){
        // 生成密钥
        SecretKey key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue());
        String hexKeyString = HexUtil.encodeHexStr(key.getEncoded());
        System.out.println("密钥："+ hexKeyString);

        // 数据准备
        String message = "Hello，这是一段需要被加解密的数据";
        System.out.println("加密前："+ message);
        // 加密
        AES aes = new AES(HexUtil.decodeHex(hexKeyString));
        String hexMessage = aes.encryptHex(message);
        System.out.println("加密后：" + hexMessage);

        // 解密
        String deMessage = aes.decryptStr(hexMessage);
        System.out.println("解密后：" + deMessage);
    }

    @Test
    public void 解密(){
        AES aes = new AES(HexUtil.decodeHex("28e08aed2b4f0e8529edbc96ecad5fbd"));
        byte[] str = aes.decrypt("rZ7A3in8JZ9hphmJjOoHfQ==");
        System.out.println(new String(str));
    }

    @Test
    public void 加密(){
        SecretKey key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue());
        System.out.println(HexUtil.encodeHexStr(key.getEncoded()));
        AES aes = new AES(key);
        String str = aes.encryptBase64("110");
        System.out.println(str);
    }
}
